const implAddress = getApp().globalData.implAddress;
Page({
  data: {
    adminiExist: false, //管理员认证
    adminiData: {
      userName: '',
      passWord: ''
    },
    isAdmini: true,
    //海报相关
    posterArray: [],
    showPoster: false,
    updatingPosterExist: false,
    updatingPoster: {},
    creatingPosterExist: false,
    //商品相关
    goodArray: [],
    showGood: false,
  },
  startAuth() {
    this.setData({
      adminiExist: !this.data.adminiExist
    })
  },
  onLoad: function () {
    const that = this
    // 初始化动画  
    this.animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out',
    })
    //获取海报
    wx.request({
      url: implAddress + 'poster/all',
      method: 'GET',
      fail: function (err) {
        console.error(err)
      },
      success: function (res) {
        that.setData({
          posterArray: res.data.data
        })
      }
    })
    //获取商品
    wx.request({
      url: implAddress + 'item/all',
      method: 'GET',
      fail: function (err) {
        console.error(err)
      },
      success: function (res) {
        that.setData({
          goodArray: res.data.data
        })
        console.log(res.data.data)
      }
    })
  },
  showModal: function () {
    // 显示模态框  
    this.setData({
      isShowModal: true
    })
    // 创建动画  
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()
    })
  },
  confirmAuth: function (e) {
    const that = this
    //显示认证中
    wx.showToast({
      title: '认证中',
      icon: 'loading'
    })
    //获取账号密码内容
    const auth = that.data.adminiData
    const globalAdminiData = getApp().globalData.adminiData;
    //检验是不是管理员
    let flag =
      auth.userName === globalAdminiData.userName &&
      auth.passWord === globalAdminiData.passWord;
    if (flag) {
      //认证成功
      wx.hideToast();
      wx.showToast({
        title: '认证成功！',
        icon: 'success'
      })
    } else {
      //认证失败
      wx.hideToast();
      wx.showToast({
        title: '认证失败！',
        icon: 'error'
      })
    }
    setTimeout(function () {
      //修改认证状态
      if (flag) {
        that.setData({
          isAdmini: !that.data.isAdmini
        })
      }
      // 认证结束，隐藏模态框  
      that.startAuth();
      wx.hideToast();
    }, 2000)
  },
  submitUsername(e) {
    const that = this
    this.setData({
      adminiData: {
        userName: e.detail.value,
        passWord: that.data.adminiData.passWord
      }
    })
  },
  submitPassword(e) {
    const that = this
    this.setData({
      adminiData: {
        userName: that.data.adminiData.userName,
        passWord: e.detail.value
      }
    })
  },
  showPoster() {
    const that = this
    this.setData({
      showPoster: !that.data.showPoster
    })
  },
  updatePoster(e) {
    const that = this
    //载入被修改的海报数据
    const id = e.currentTarget.dataset.id - 1
    this.data.posterArray.forEach((item, index) => {
      if (index == id) {
        that.setData({
          updatingPoster: item
        })
      }
    })
    //显示输入框
    this.setData({
      updatingPosterExist: !that.data.updatingPosterExist
    })
  },
  confirmUpdatePoster() {
    const that = this
    //显示修改中
    wx.showToast({
      title: '修改中',
      icon: 'loading'
    })
    //修改内容
    // wx.request({
    //   url: 'url',
    //   fail: function (err) {
    //     console.error(err)
    //     wx.showToast({
    //       title: '修改失败',
    //       icon:'error'
    //     })
    //   },
    //   success:function(res){
    //     wx.showToast({
    //       title: '修改成功',
    //       icon:'success'
    //     })
    //   }
    // })
    setTimeout(function () {
      //去除输入框
      that.setData({
        updatingPosterExist: !that.data.updatingPosterExist
      })
      wx.hideToast()
    }, 3000)
  },
  submitPosterName(e) {
    const updating = this.data.updatingPoster
    const data = e.detail.value
    this.setData({
      updatePoster: {
        img_content: updating.img_content,
        img_id: updating.id,
        poster_name: data
      }
    })
  },
  delectPoster(e) {
    const that = this
    //载入被修改的海报数据
    const id = e.currentTarget.dataset.id - 1
    this.data.posterArray.forEach((item, index) => {
      if (index == id) {
        that.setData({
          updatingPoster: item
        })
      }
    })
    //问是否删除
    wx.showModal({
      title: '删除海报',
      content: '是否删除' + that.data.updatingPoster.poster_name + '？',
      complete: (res) => {
        if (res.confirm) {
          //显示删除中
          wx.showToast({
            title: '删除中',
            icon: 'loading'
          })
          //删除
          // wx.request({
          //   url: implAddres +'url',
          //   fail:function(err){
          //     console.error(err)
          //     wx.showToast({
          //       title: '删除失败',
          //       icon:'error'
          //     })
          //   },
          //   success:function(res){
          //     wx.showToast({
          //       title: '删除成功',
          //       icon:'success'
          //     })
          //   }
          // })
          //关闭模态框
          setTimeout(() => {
            wx.hideToast()
          }, 3000)
        }
      }
    })
  },
  cancelUpdatePoster() {
    const that = this
    //显示输入框
    this.setData({
      updatingPosterExist: !that.data.updatingPosterExist
    })
  },
  createPoster(){
    const that = this;
    that.setData({
      creatingPosterExist:!that.data.creatingPosterExist
    })
  }
})